package com.softaria.spkiller.dependencies;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import classycle.ClassAttributes;
import classycle.graph.AtomicVertex;
import classycle.graph.Attributes;
import classycle.graph.Vertex;

import com.softaria.spkiller.metadata.ClassIdentifier;
import com.softaria.spkiller.metadata.ClassIdentifierImpl;

class ClassycleDependencyGraph implements DependencyGraph {

	private Collection<ClassIdentifier> allClasses = new ArrayList<ClassIdentifier>();
	private Map<ClassIdentifier,Collection<ClassIdentifier>> usedClasses = new HashMap<ClassIdentifier, Collection<ClassIdentifier>>();

	public ClassycleDependencyGraph(AtomicVertex[] vertexes) {
		for(AtomicVertex vertex: vertexes) {
			Attributes attrs = vertex.getAttributes();
			if(attrs instanceof ClassAttributes) {
				ClassAttributes classAttrs = (ClassAttributes)attrs;
				
				ClassIdentifier classID = new ClassIdentifierImpl(classAttrs.getName());
				
				allClasses.add(classID);
				
				Collection<ClassIdentifier> usedClasses = new ArrayList<ClassIdentifier>();

				this.usedClasses.put(classID, usedClasses);
				
				for (int i = 0; i < vertex.getNumberOfOutgoingArcs(); i++) {
					Vertex subVertex = vertex.getHeadVertex(i);
					Attributes subAttrs = subVertex.getAttributes();
					
					if(subAttrs instanceof ClassAttributes) {
						ClassAttributes subClassAttrs = (ClassAttributes)subAttrs;
						
						ClassIdentifier subClassID = new ClassIdentifierImpl(subClassAttrs.getName());

						usedClasses.add(subClassID);
					}
					
				}
				
			}
			
		}
	}

	@Override
	public Collection<ClassIdentifier> getAllClasses() {
		return allClasses;
	}

	@Override
	public Collection<ClassIdentifier> getUsedClasses(ClassIdentifier user) {
		return usedClasses.get(user);
	}

}
